<template>
    <div class="issueDetails-view">
      <div style="min-width: 1100px">
        <modules-title titleName="待发药详情">

          <template slot="btn">
            <el-button v-if="datas.id" class="main-btn" type="primary" @click="issueClick">发药</el-button>
          </template>
        </modules-title>

        <div class="scroll-view" v-if="datas.id">

          <div class="patientView">

            <div class="title">诊断详情</div>

            <div class="bese-information">
              <div class="information-item">
                <span class="grayTitle">患者：</span>
                <span class="value">{{datas.real_name}}</span>
              </div>

              <div class="information-item">
                <span class="grayTitle">性别：</span>
                <span class="value" v-if="datas.charge_id && datas.gender == 0">男</span>
                <span class="value" v-else-if="datas.charge_id">女</span>
              </div>

              <div class="information-item">
                <span class="grayTitle">年龄：</span>
                <span class="value">{{datas.age | dealAge}}</span>
              </div>

              <div class="information-item">
                <span class="grayTitle">医生：</span>
                <span class="value">{{datas.doctor_name}}</span>
              </div>

              <div class="information-item" v-if="datas.department_name && datas.department_name != ''">
                <span class="grayTitle">科室：</span>
                <span class="value">{{datas.department_name}}</span>
              </div>

              <div class="information-item">
                <span class="grayTitle">就诊日期：</span>
                <span class="value" v-if="datas.created_at.length > 0">{{datas.created_at.substring(0,10) + " " + datas.created_at.substring(11,19)}}</span>
              </div>

              <div class="information-item">
                <span class="grayTitle">就诊类型：</span>
                <span class="value" v-if="datas.medical_record_repeat == 2">复诊</span>
                <span class="value" v-else-if="datas.medical_record_repeat == 1">初诊</span>
                <span class="value" v-else>无</span>
              </div>
            </div>

            <div class="total-view">
              <div class="information-item">
                <span class="grayTitle">诊断：</span>
                <span class="value">{{datas.result}}</span>
              </div>
            </div>
          </div>

          <!--        <check-item v-if="datas.check_item  && datas.check_item.length > 0" :datas="datas.check_item" :type="type" @dataChange="checkChange"></check-item>-->

          <!--        <surcharge-item v-if="datas.surcharge  && datas.surcharge.length > 0" :datas="datas.surcharge" :type="type" @dataChange="surchargeChange"></surcharge-item>-->

          <prescription-item v-for="i in datas.prescription_ary_info" :data="i" :method="datas.sell_method" v-model="i.details" :type="type" @dataChange="prescriptionChange"></prescription-item>

        </div>

        <blank v-else picWidth="300px"></blank>
      </div>



    </div>
</template>

<script>
  import {medicineIssueDetail,medicineInventory} from "../../../api/payOrDrug";
  import surchargeItem from "../../payOrDrug/modules/surchargeItem";
    import checkItem from "../../payOrDrug/modules/checkItem";
    import prescriptionItem from "../../payOrDrug/modules/prescriptionItem";

    export default {
        name: "issueDetails",
        components:{prescriptionItem,checkItem,surchargeItem},
        data:function () {
            return{
                type:3,
                value:'',
                checked:false,
                preferentialValue:0,
                datas:{
                    amount:0,
                    created_at:'',
                },
                issueDatas:{},
                total:0,
            }
        },
        computed: {
            amount: function(){//test为计算属性，调用时和调用属性一样调用test即可
                let am = 0;
                am = this.total - this.preferentialValue;
                return am//msg是依赖属性，改变时会执行test函数
            }
        },
        mounted:function(){

        },
        methods:{
            clearData:function(){
                this.datas = {
                    amount:0,
                    created_at:'',
                }
                this.issueDatas = {};
                this.total = 0;
                this.preferentialValue = 0;
            },
            issueClick:function () {
                let isIssue = false;
                let number = 0;
                for (let i = 0; i < this.issueDatas.prescription_ary_info.length; i++){
                    let data = this.issueDatas.prescription_ary_info[i];

                    if (data.details && data.details.length > 0){
                        number += data.details.length;
                        isIssue = true;
                    }
                }

                let number1 = 0;
                for (let i = 0; i < this.datas.prescription_ary_info.length; i++){
                    let data = this.datas.prescription_ary_info[i];

                    if (data.details && data.details.length > 0){
                        number1 += data.details.length;
                    }
                }

                this.issueDatas.is_finish_medicine = number == number1 ? 2 : 1;

                if (!isIssue){
                    this.$message.error("请选择药品");
                    return;
                }
                this.issueDatas.id = this.issueDatas.charge_id

                medicineInventory(this.issueDatas).then((res)=>{
                    if (res.code == 200){
                        this.$message({
                            duration:1000,
                            message: '发药成功',
                            type: 'success'
                        });

                        if (this.issueDatas.is_finish_medicine == 2){
                            this.clearData();
                            this.$emit("update-list")
                        }else {
                            let id = this.datas.charge_id
                            this.clearData();
                            this.getData(id);
                        }
                    }else {
                        this.$message.error(res.msg);
                    }
                }).catch((err)=>{

                })
            },
            getData:function (id) {
                medicineIssueDetail(id).then((res)=>{
                    if (res.code == 200){
                        this.datas = res.data;
                        this.issueDatas = {...res.data};
                        this.issueDatas.prescription_ary_info = []
                    }else {
                        this.$message.error(res.msg);
                    }
                }).catch((err)=>{

                })
            },
            prescriptionChange:function (list) {
                console.log("prescriptionChange")
                console.log(list);
                let index = -1;
                for (let i = 0; i < this.issueDatas.prescription_ary_info.length; i++){
                    if (this.issueDatas.prescription_ary_info[i].id == list.id){
                        index = i;
                    }
                }
                if (index != -1){
                    this.issueDatas.prescription_ary_info[index] = list;
                }else {
                    this.issueDatas.prescription_ary_info.push(list);
                }
            },
            checkChange:function(list){
                this.payDatas.check_item = list;
                console.log(this.payDatas)
                this.totalChange();
            },
            surchargeChange:function(list){
                this.payDatas.surcharge = list;

            },
        }
    }
</script>

<style lang="scss" scoped>
  @import "@/assets/scss/issueDrug/issueDetails.scss";
</style>
